package com.z0ltrix.SMSitSupport;

import java.util.Calendar;
import java.util.Date;

/*import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.SmsMessage;
import android.util.Log;

public class SMSNotifyRx extends BroadcastReceiver {
	
	private final static String SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED";
	private static final String TAG = "GENTRAN_SMS_SILENCER";
	private static final int DELAY_IN_MILLISECS = 5000;
	public static AudioManager audio;
	SharedPreferences myPrefs;
	public static String MY_PREFS = "MY_PREFS";
	private int startTimeHour;
	private int startTimeMin;
	private int endTimeHour;
	private int endTimeMin;
	private String SMSKeyword;

	@Override
	public void onReceive(Context context, Intent intent) {
		
		if(intent.getAction().equals(SMSNotifyRx.SMS_ACTION)){
			
			String body = "";
			//String from = "";
			//StringBuilder sb = new StringBuilder();
			
			Bundle bundle = intent.getExtras();
			
			if(bundle!=null){
				
				Object[] pdus = (Object[]) bundle.get("pdus");
				
				for (Object pdu : pdus){
					SmsMessage message = SmsMessage.createFromPdu((byte[])pdu);
					//sb.append("Received SMS\nFrom: ");
					//from = message.getDisplayOriginatingAddress();
					//sb.append(from);
					//sb.append("----Message----");
					body = message.getDisplayMessageBody();
					//sb.append(body);
				}
				
			}
			
			//Log.v(TAG, "[SMSAPP] - " + sb);
			//Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
			
			myPrefs = context.getSharedPreferences(MY_PREFS, Context.MODE_PRIVATE);
			startTimeHour = myPrefs.getInt("startTimeHour", 8);
			startTimeMin  = myPrefs.getInt("startTimeMin", 0);
			endTimeHour = myPrefs.getInt("endTimeHour", 20);
			startTimeMin  =myPrefs.getInt("endTimeMin", 0);
			SMSKeyword = myPrefs.getString("SMSKeyword", "Gentran");
			
			if(GentranPage(body, SMSKeyword)){
				
				if(!isSupportTime(startTimeHour, startTimeMin, endTimeHour, endTimeMin)){
								
				audio = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
				final int ringerMode = audio.getRingerMode();
				audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
				
				Handler handler = new Handler();
				handler.postDelayed(new Runnable() {
					
					public void run() {
						
						audio.setRingerMode(ringerMode);
					}
				}, DELAY_IN_MILLISECS);
				
				
				/*NotificationManager nm  =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
				CharSequence ticker = "New Page from: " + from + ": " + body;
				long when = System.currentTimeMillis();
				Notification notification = new Notification(R.drawable.icon, ticker, when);
				
				CharSequence contentTitle = "New Gentran Page";
				CharSequence contentMessage = sb.toString();
				
				Intent intentNotify = new Intent();
				
				PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intentNotify, 0);
				
				notification.setLatestEventInfo(context, contentTitle, contentMessage, contentIntent);
					
				nm.notify(0x1221, notification);*/
				
				
				
				}
			}
			
		}
		

	}

	

	private boolean isSupportTime(int startTimeH, int startTimeM, int endTimeH, int endTimeM) {
		
		Calendar currentTime = Calendar.getInstance();
		Date now = new Date();
		currentTime.add(Calendar.DAY_OF_YEAR, 0);
		currentTime.set(Calendar.HOUR_OF_DAY, now.getHours());
		currentTime.set(Calendar.MINUTE, now.getMinutes());
		currentTime.set(Calendar.SECOND, now.getSeconds());
	
		
		Calendar supportTimeStart = Calendar.getInstance();
		supportTimeStart.add(Calendar.DAY_OF_YEAR, 0);
		supportTimeStart.set(Calendar.HOUR_OF_DAY, startTimeH);
		supportTimeStart.set(Calendar.MINUTE, startTimeM);
		supportTimeStart.set(Calendar.SECOND, 0);
		
		Calendar supportTimeEnd = Calendar.getInstance();
		supportTimeEnd.add(Calendar.DAY_OF_YEAR, 0);
		supportTimeEnd.set(Calendar.HOUR_OF_DAY, endTimeH);
		supportTimeEnd.set(Calendar.MINUTE, endTimeM);
		supportTimeEnd.set(Calendar.SECOND, 0);
		
		Log.v(TAG, "currentTime: " + currentTime.getTime().toString() );
		Log.v(TAG, "supportTimeStart: " + supportTimeStart.getTime().toString());
		Log.v(TAG, "supportTimeEnd: " + supportTimeEnd.getTime().toString());
		
		if(currentTime.after(supportTimeStart) && currentTime.before(supportTimeEnd))
			return true;
		else
			return false;
	}

	private boolean GentranPage(String body, String SMSkeywordSearch) {
	
		if(body.contains(SMSkeywordSearch)){
			//Log.v(TAG, "--------------------Gentran page: /" + body + "/ :+0000000000:");
			return true;
		}
			
		else{
			//Log.v(TAG, "--------------------NOT Gentran page: /" + body + "/ :+0000000000:");
			return false;
		}
			
	}

}
